﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace DistributeTransaction.Utils
{
    public sealed class StringUtils
    {
        internal static bool ContainsIgnoreCase(string s, string subString)
        {
            if (!string.IsNullOrEmpty(subString) && !string.IsNullOrEmpty(s))
            {
                return s.ToUpperInvariant().Contains(subString.ToUpperInvariant());
            }
            return false;
        }


        internal static bool IsNumber(string s)
        {
            Regex reg = new Regex("^[-]?[0-9]+?$");
            return reg.IsMatch(s);
        }

        internal static string Trim(string text)
        {
            return string.IsNullOrEmpty(text) ? text : text.Trim();
        }

        public static string RemovePipeLine(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return s.Replace("|", string.Empty);
            }
            return s;
        }

        public static string ForamtDateTime(object o)
        {
            if (o != DBNull.Value && o is DateTime)
            {
                DateTime dt = (DateTime)o;
                return dt.ToString("yyyyMMdd");
            }
            return string.Empty;
        }

        public static string ForamtString(object o)
        {
            return o == DBNull.Value ? string.Empty : o.ToString().Trim();
        }

        /// <summary>
        /// If a column name contains any of these special characters 
        /// ~ ( ) # \ / = > < + - * % & | ^ ' " [ ], 
        /// you must enclose the column name within square brackets [ ]. 
        /// If a column name contains right bracket ] or backslash \, 
        /// escape it with backslash (\] or \\).
        /// </summary>
        /// <param name="valueWithoutWildcards"></param>
        /// <returns></returns>
        public static string EscapeLikeValue(string valueWithoutWildcards)
        {
            valueWithoutWildcards = StringUtils.Trim(valueWithoutWildcards);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < valueWithoutWildcards.Length; i++)
            {
                char c = valueWithoutWildcards[i];
                switch (c)
                {
                    case '~':
                    case '(':
                    case ')':
                    case '#':
                    case '/':
                    case '=':
                    case '>':
                    case '<':
                    case '+':
                    case '-':
                    case '*':
                    case '%':
                    case '&':
                    case '|':
                    case '^':
                    case '"':
                    case '[':
                    case ']':
                    case '\\':
                        sb.Append("[").Append(c).Append("]");
                        break;
                    //case '\\':
                    //    sb.Append("\\").Append(c);
                    //    break;
                    case '\'':
                        sb.Append("''");
                        break;
                    default:
                        sb.Append(c);
                        break;

                }
                //if (c == '*' || c == '%' || c == '[' || c == ']')
                //    sb.Append("[").Append(c).Append("]");
                //else if (c == '\'')
                //    sb.Append("''");
                //else
                //    sb.Append(c);
            }
            return sb.ToString();
        }
    }
}